import base64

import scrapy

from scrapy.loader import ItemLoader

from ts.items.Face import Face


class FaceYouMaSpider(scrapy.Spider):
    name = "face_youma"

    def __init__(self, name=None, **kwargs):
        super().__init__(name, **kwargs)
        self.host = 'https://www.xwb2uooia5o59avwxb.com/'
        self.page = 1
        self.maxpage = 201

    def start_requests(self):
        yield scrapy.Request(url=self.host + '/actresses/' + str(self.page), callback=self.parse)

    def parse(self, response):
        print('当前是第' + str(self.page) + '页')
        for item in response.css('#waterfall a'):

            img = item.css('div.photo-frame img')
            imgurl = img.css('::attr(src)').extract_first()
            if 'nowprinting.gif' in imgurl:  # 跳过没图片的
                continue

            item_loader = ItemLoader(item=Face(), selector=item)
            item_loader.add_css('videourl', '::attr(href)')
            item_loader.add_value('imgurl', imgurl)
            item_loader.add_value('username', img.css('::attr(title)').extract_first())

            yield scrapy.Request(url=item_loader.load_item()['imgurl'], meta={'loader': item_loader},
                                 callback=self.parse_img)

        next_page = response.css('#next')
        if next_page:
            next_url = next_page.css('a::attr(href)').extract_first()
            self.page = int(next_url.split('/')[-1])
            if self.page <= self.maxpage:
                yield scrapy.Request(url=self.host + next_url, callback=self.parse)

    def parse_img(self, response):
        item_loader = response.meta['loader']
        if item_loader:
            buf = response.body
            item_loader.add_value('buf', bytes.decode(base64.b64encode(buf)))
        yield item_loader.load_item()
